//
//  LLMallViewController.m
//  lrlzapp
//
//  Created by dl on 16/2/22.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLMallViewController.h"
#import "LLMallViewModel.h"

#import "LLMallHotActivityCollectionViewCell.h"
#import "LLMallCatalogCollectionViewCell.h"
#import "LLMallBrandCollectionViewCell.h"
#import "LLGoodsConnectionViewCell.h"
#import "LLMallHeaderView.h"
#import "LLMallDaytopHeaderView.h"
#import "LLMallFooterView.h"
#import "LLRefresh.h"

#define IDENTIFIER_CELL_MALL_HOT_ACTIVITY           @"IDENTIFIER_CELL_MALL_HOT_ACTIVITY"
#define IDENTIFIER_CELL_MALL_CATALOG                @"IDENTIFIER_CELL_MALL_CATALOG"
#define IDENTIFIER_CELL_MALL_BRAND                  @"IDENTIFIER_CELL_MALL_BRAND"
#define IDENTIFIER_CELL_MALL_DAY_TOP                @"IDENTIFIER_CELL_MALL_DAY_TOP"
#define IDENTIFIER_HEADER_MALL                      @"IDENTIFIER_HEADER_MALL"
#define IDENTIFIER_HEADER_MALL_DAY_TOP              @"IDENTIFIER_HEADER_MALL_DAY_TOP"
#define IDENTIFIER_FOOTER_MALL                      @"IDENTIFIER_FOOTER_MALL"

@interface LLMallViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property(weak, nonatomic) IBOutlet UIView *searchBarView;
@property(weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property(nonatomic, strong, readwrite) LLMallViewModel *viewModel;

@end

@implementation LLMallViewController
@dynamic viewModel;

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.viewModel = [[LLMallViewModel alloc] init];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    self.searchBarView.frame = CGRectMake(0, 5, FULL_WIDTH - 85 * SUIT_SCALE_ALL, 30);
    self.searchBarView.layer.cornerRadius = 3;

    [self.collectionView registerClass:[LLMallHotActivityCollectionViewCell class]
            forCellWithReuseIdentifier:IDENTIFIER_CELL_MALL_HOT_ACTIVITY];
    [self.collectionView registerClass:[LLMallCatalogCollectionViewCell class]
            forCellWithReuseIdentifier:IDENTIFIER_CELL_MALL_CATALOG];
    [self.collectionView registerClass:[LLMallBrandCollectionViewCell class]
            forCellWithReuseIdentifier:IDENTIFIER_CELL_MALL_BRAND];
    [self.collectionView registerClass:[LLGoodsConnectionViewCell class]
            forCellWithReuseIdentifier:IDENTIFIER_CELL_MALL_DAY_TOP];

    [self.collectionView registerClass:[LLMallHeaderView class]
            forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                   withReuseIdentifier:IDENTIFIER_HEADER_MALL];
    [self.collectionView registerClass:[LLMallDaytopHeaderView class]
            forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                   withReuseIdentifier:IDENTIFIER_HEADER_MALL_DAY_TOP];
    [self.collectionView registerClass:[LLMallFooterView class]
            forSupplementaryViewOfKind:UICollectionElementKindSectionFooter
                   withReuseIdentifier:IDENTIFIER_FOOTER_MALL];

    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *) self.collectionView.collectionViewLayout;
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    @weakify(self)
    _collectionView.header = [LLRefreshNomalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        [self.viewModel.loadMenusCommand execute:nil];
    }];
}

- (void)bindViewModel {
    [super bindViewModel];

    @weakify(self)
    [RACObserve(self.viewModel, dataSource)
            subscribeNext:^(id _) {
                @strongify(self)
                [self.collectionView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 4)]];
            }];

    [[RACObserve(self.viewModel, isLoading)
            distinctUntilChanged]
            subscribeNext:^(NSNumber *isLoading) {
                @strongify(self)
                if ([isLoading boolValue]) {
                    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES].labelText = @"Logging in...";
                } else {
                    [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
                    [self.collectionView.header endRefreshing];
                }
            }];

    [self.viewModel.loadMenusCommand execute:nil];
}

#pragma mark -
#pragma mark UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 4;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    id cellViewModel = [self.viewModel.dataSource objectAtIndex:section];
    if (section == 0 && [[cellViewModel itemList] count] > 0) {
        return 1;
    } else if (section == 1 && [[cellViewModel itemList] count] > 0) {
        return 1;
    } else if (section == 2) {
        return [[cellViewModel itemList] count];
    } else if (section == 3) {
        return [cellViewModel count];
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        LLMallHotActivityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IDENTIFIER_CELL_MALL_HOT_ACTIVITY
                                                                                              forIndexPath:indexPath];
        LLMenuItemDataCollectionViewModel *cellViewModel = [self.viewModel.dataSource objectAtIndex:indexPath.section];
        [cell bindViewModel:cellViewModel];
        return cell;
    } else if (indexPath.section == 1) {
        LLMallCatalogCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IDENTIFIER_CELL_MALL_CATALOG
                                                                                          forIndexPath:indexPath];
        LLMenuItemDataCollectionViewModel *cellViewModel = [self.viewModel.dataSource objectAtIndex:indexPath.section];
        [cell bindViewModel:cellViewModel];
        return cell;
    } else if (indexPath.section == 2) {
        LLMallBrandCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IDENTIFIER_CELL_MALL_BRAND
                                                                                        forIndexPath:indexPath];
        LLMenuItemDataCollectionViewModel *cellViewModel = [self.viewModel.dataSource objectAtIndex:indexPath.section];
        [cell bindViewModel:[cellViewModel.itemList objectAtIndex:indexPath.item]];
        return cell;
    } else {
        LLGoodsConnectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IDENTIFIER_CELL_MALL_DAY_TOP
                                                                                    forIndexPath:indexPath];
        LLGoodsBasicViewModel *cellViewModel = [[self.viewModel.dataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.item];
        [cell bindViewModel:cellViewModel];
        return cell;
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        if (indexPath.section >= 0 && indexPath.section <= 2) {
            LLMallHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                              withReuseIdentifier:IDENTIFIER_HEADER_MALL
                                                                                     forIndexPath:indexPath];
            RAC(headerView.titleLabel, text) = [[RACObserve(self.viewModel, dataSource)
                    takeUntil:headerView.rac_prepareForReuseSignal]
                    map:^id(NSArray *dataSource) {
                        LLMenuItemDataCollectionViewModel *cellViewModel = [dataSource objectAtIndex:indexPath.section];
                        return cellViewModel.title;
                    }];
            return headerView;
        } else {
            LLMallDaytopHeaderView *headerView =
                    [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                       withReuseIdentifier:IDENTIFIER_HEADER_MALL_DAY_TOP
                                                              forIndexPath:indexPath];
            return headerView;
        }
    } else {
        LLMallFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:IDENTIFIER_FOOTER_MALL forIndexPath:indexPath];
        return footerView;
    }
}

#pragma mark -
#pragma mark UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}

#pragma mark -
#pragma mark UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(FULL_WIDTH, SUIT_H(98) + SUIT_H(12));
    } else if (indexPath.section == 1) {
        return CGSizeMake(FULL_WIDTH, SUIT_H_320(116));
    } else if (indexPath.section == 2) {
        return CGSizeMake(FULL_WIDTH / 4.f, SUIT_H_320(73.f));
    } else if (indexPath.section == 3) {
        CGFloat cellWidth = (FULL_WIDTH - 3 * SUIT_W(5)) / 2.f;
        return CGSizeMake(cellWidth, cellWidth + SUIT_H(85));
    }
    return CGSizeZero;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    if (section == 3) {
        return UIEdgeInsetsMake(0, SUIT_W(5), 0, SUIT_W(5));
    }
    return UIEdgeInsetsZero;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 3) {
        return SUIT_H(5);
    }
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    id cellViewModel = [self.viewModel.dataSource objectAtIndex:section];
    if (section >= 0 && section <= 2) {
        if ([[cellViewModel itemList] count] > 0) {
            return CGSizeMake(FULL_WIDTH, SUIT_H(38));
        }
    } else if (section == 3) {
        if ([cellViewModel count] > 0) {
            return CGSizeMake(FULL_WIDTH, SUIT_H(29) + SUIT_H_320(35));
        }
    }
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    if (section >= 0 && section <= 2) {
        LLMenuItemDataCollectionViewModel *cellViewModel = [self.viewModel.dataSource objectAtIndex:section];
        if (cellViewModel.itemList.count > 0) {
            return CGSizeMake(FULL_WIDTH, SUIT_H(8));
        }
    }
    return CGSizeZero;
}

@end
